home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / s0ftpj / gork-2.0b.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-17  |  19.9 KB  |  856 lines

  1. /*
  2.  * G o r k   -  U n i x   P a c k e t   L o g g e r
  3.  * 
  4.  * v e r s i o n  2.0b - bugs fixed
  5.  * 
  6.  * Questo sorgente non e' coperto da nessun copyright e come tale potete farci
  7.  * quello che volete... visto che non arrichisce le mie tasche potete
  8.  * tranquillamente rubarlo all'autore che tanto a lui non gliene frega un
  9.  * cazzo... ne' io, ne' il ladro ci possiamo ritenere responsabili di quello
  10.  * che fate con questo sorgente... 
  11.  *
  12.  * Compile with: gcc gork.c -Wall -lpcap
  13.  *
  14.  * Tested on:
  15.  *     Linux RedHat 6.X     ( mail.cameretta.pig )
  16.  *    Linux Debian 2.0r3   ( porcellino.cameretta.pig ) 
  17.  *      FreeBSD 4.0          ( sp00f.y0ur.life.cameretta.pig )
  18.  *
  19.  * - Now it logs correctly 
  20.  *
  21.  * pIGpEN [pigpen@s0ftpj.org]
  22.  */
  23.  
  24. /*
  25.  * Questa e' la versione che permette di loggare i pacchetti rispetto alla 
  26.  * versione pubblicata su bfi 7:
  27.  *
  28.  * - e' stata corretta la hostLookup()
  29.  * - HST_FOUND viene inizializzata dentro il ciclo in modo da permettere di
  30.  *   loggare solo sui files giusti
  31.  * - il codice e' stato identato come dio bit comanda e sono state tolte 
  32.  *   alcune parti superflue tra cui una variabile in piu' utilizzata per
  33.  *   leggere l'header pcap c'e' ancora una che non serve un cazzo ma che
  34.  *   viene passata come parametro alla funzione pcap_next() 
  35.  */
  36.  
  37.  
  38. // CONFIGURATION 
  39.  
  40. /*
  41.  * DONT_LOOKUP -> if you have problems with gork & your dns
  42.  * SYSTEM_LOG  -> Log gork msg via syslog warning this can give problems
  43.  * PROMISC     -> 1 to activate it 0 if you wanna see only your box pkts
  44.  */
  45.  
  46. #define PROMISC 1
  47.  
  48. /* 
  49.  *
  50.  * G o R K  a porting of Timothy Leary on your Box ... ;)
  51.  * 
  52.  */
  53.  
  54. #include         <stdio.h>
  55. #include         <netdb.h>
  56. #include         <string.h>
  57. #include         <stdlib.h>
  58. #include         <unistd.h>
  59. #include         <sys/ioctl.h>
  60. #include         <sys/types.h>
  61. #include         <sys/time.h>
  62. #include         <sys/signal.h>
  63. #include         <sys/socket.h>
  64. #include        <netinet/in_systm.h>
  65. #include         <netinet/in.h>
  66. #include         <pcap.h>        
  67. #if (linux)
  68. #define __FAVOR_BSD
  69. #endif
  70. #include        <netinet/ip.h>
  71. #include        <netinet/ip_icmp.h>
  72. #include        <netinet/udp.h>
  73. #include        <arpa/inet.h>
  74. #include        <arpa/nameser.h>
  75.  
  76. #ifdef SYSTEM_LOG
  77. #include        <syslog.h>
  78. #endif
  79.  
  80. #define            CONF        "gork.conf"
  81. #define            LOG_TYPE    4    //syslog
  82.  
  83. #define            SYSLOG_PORT    514
  84. #define            MTU        1500
  85. #define            URG        32
  86. #define            ACK_PSH        24
  87. #define            SYN_ACK        18
  88. #define            FIN_ACK        17
  89. #define            ACK        16
  90. #define                 PSH             8
  91. #define                 RST             4
  92. #define                 SYN             2
  93. #define             FIN             1
  94.  
  95.  
  96. #define            WHITE        printf("\033[0;29m")
  97. #define                 RED             printf("\033[1;31m")
  98. #define             GREEN           printf("\033[1;32m")
  99. #define                YELLOW          printf("\033[1;33m")
  100. #define            BLUE        printf("\033[1;34m")
  101. #define            MAGENTA        printf("\033[1;35m")
  102. #define             CYAN            printf("\033[1;36m")
  103. #define            RETURN        printf("\n")
  104. #define            CLEAR        printf("\033[2J\033[1;1H")
  105. #define            LINE        printf("<\033[1;32m-----\033[1;34m>\n");
  106.  
  107. #define         ADDR_DIM    255    
  108. #define            LOG_ALL        "all_g0rk.log"
  109. #define            LOG_SL        "gork.syslog"
  110.  
  111. unsigned char         s_addr[ADDR_DIM];
  112. unsigned char       d_addr[ADDR_DIM];
  113. char             *hst_saddr=NULL;
  114. char             *hst_daddr=NULL;
  115. time_t             now;
  116. char             date[60];
  117. int             port=0;
  118. char            *syslog_string;
  119.  
  120. extern char         *optarg;
  121.  
  122. pcap_t                 *pcap_global_descriptor;
  123. char                   *deviceglobal=NULL;
  124. int                    offset;
  125.  
  126. struct packet_info{
  127.     unsigned char ttl, protocol, version;
  128.     unsigned char *saddr, *daddr;
  129.     unsigned long seq, ack_seq;
  130.     unsigned short source, dest, type, id, flags, window;
  131.     char dataload[MTU];
  132. };
  133.  
  134. struct DNSpkt {
  135.  HEADER head;
  136.  char query[255];
  137. };
  138.  
  139. struct TCPhdr {
  140.  u_short source, dest;
  141.  u_int32_t seq, ack_seq;
  142.  u_short offset_flag, window, checksum, urgent;
  143. };
  144.  
  145. int         main             __P((int, char **));
  146. void         usage             __P((char *));
  147. void         scan             __P((struct packet_info));
  148. void         fuckin_about_all_day     __P((void));
  149. void         print_addr         __P((struct packet_info));
  150. void         pcap_device_on         __P((void));
  151. void         sniff_pk         __P((struct packet_info *));
  152. void         ethclose         __P(());
  153. void         dump_tcp         __P((struct packet_info, int));
  154. void         dump_udp         __P((struct packet_info));
  155. void         dump_icmp         __P((struct packet_info));
  156.  
  157. #ifndef DONT_LOOKUP
  158.  
  159. char         *hostLookup         __P((struct in_addr));
  160.  
  161. char *hostLookup(struct in_addr in)
  162. {
  163.  struct hostent *hostEnt;
  164.  
  165.  if((hostEnt=gethostbyaddr((char *)&in, sizeof(struct in_addr), AF_INET)))
  166.      return (strdup(hostEnt->h_name));
  167.       return NULL;
  168. }
  169.  
  170. #endif
  171.  
  172.  
  173. void usage(char *arg)
  174. {
  175.  YELLOW;
  176.  printf("\t\t\t  pIGpEN - diGiTaL dEAdhEAd -");
  177.  BLUE;
  178.  printf("\n\n\nPut hostname/ip in gork.conf ... \nUse ");
  179.  MAGENTA;
  180.  printf("-p dest_port ");
  181.  BLUE;
  182.  printf("if you wanna log only packets to dest_port\n and with ip/hostname in");
  183.  MAGENTA;
  184.  printf(" gork.conf\n");
  185.  BLUE;  
  186.  printf("To log all source ip put in gork.conf: ");
  187.  MAGENTA;
  188.  printf(".\n");
  189.  printf("-l string ");
  190.  BLUE;
  191.  printf("write in gork.syslog if <string> was found in syslog messages\n");
  192.  printf("Other options: \n");
  193.  printf("        -i interface\n");
  194.  printf("        -v verbose mode for tcp\n");
  195.  WHITE;
  196. }
  197.  
  198. void fuckin_about_all_day(void)
  199. {
  200.  CLEAR;
  201.  fflush(stdout); sleep(1);
  202.  printf("\033[1;35m     .g#S$'$S#n.\n");
  203.  printf("\033[1;35m     $$$$$ $$$$'\n");
  204.  printf("\033[1;35m     $$$$$\n");
  205.  printf("\033[1;35m     `$$$$$$$$$n\n");
  206.  printf("\033[1;34m           $$$$$\n");
  207.  printf("\033[1;34m     $$$$$ $$$$$\n");
  208.  printf("\033[1;34m     `$$$$s$$$S'\n\n");
  209.  fflush(stdout); sleep(1);
  210.  printf("\033[1;35m     .g#S$'$S#n.\n");
  211.  printf("\033[1;35m     $$$$$ $$$$$\n");
  212.  printf("\033[1;35m     $$$$$ $$$$$\n");
  213.  printf("\033[1;35m     $$$$$ $$$$$\n");
  214.  printf("\033[1;34m     $$$$$s$$$$'\n");
  215.  printf("\033[1;34m     $$$$$      \n");
  216.  printf("\033[1;34m     $$$$       \n");
  217.  fflush(stdout); sleep(1);
  218.  printf("\033[1;35m            S#n.\n");
  219.  printf("\033[1;35m            $$$$\n");
  220.  printf("\033[1;35m            $$$$\n");
  221.  printf("\033[1;35m            $$$$\n");
  222.  printf("\033[1;34m            $$$$\n");
  223.  printf("\033[1;34m      $$$$$ $$$$\n");
  224.  printf("\033[1;34m      `$$$$s$$S'\n\n");
  225.  fflush(stdout); sleep(1);
  226.  
  227.  MAGENTA;
  228.  printf("\033[15A\t\t\t\t     _____________________\n");
  229.  fflush(stdout); sleep(1);
  230.  printf("\033[01A\t\t\t\t     s o f t p r o j e c t\n\n");
  231.  fflush(stdout); sleep(1);
  232.  BLUE;
  233.  printf("\t\t\t  ____________________________________________\n\n");
  234.  fflush(stdout); sleep(1);
  235.  printf("\t\t\t\033[02A  d i g i t a l  s e k u r i t y  f o r  y 2 k\n\n");
  236.  fflush(stdout); sleep(1);
  237.  printf("\t\t\t\t  ___________________________\n");
  238.  fflush(stdout); sleep(1);
  239.  printf("\t\t\t\t\033[01A  w w w . s 0 f t p j . o r g\n");
  240.  fflush(stdout); sleep(1);
  241.  sleep(3);
  242.  CLEAR;
  243. }
  244.  
  245. void print_addr(struct packet_info infoz)
  246. {
  247.  struct servent  *service = NULL;
  248.  struct protoent *proto   = NULL;
  249.  struct in_addr  in_a;
  250.  int yes_lookup=0;
  251.  
  252.  now=time(NULL);
  253.  strftime(date,60,"%H:%M:%S %a %h %d", localtime(&now));
  254.  
  255.  bzero(s_addr,sizeof(s_addr)); 
  256.  bzero(d_addr,sizeof(d_addr));
  257.  hst_daddr=NULL;
  258.  hst_saddr=NULL;
  259.  
  260.  sprintf(s_addr,"%u.%u.%u.%u",
  261.                 infoz.saddr[0],
  262.                 infoz.saddr[1],
  263.                 infoz.saddr[2],
  264.                 infoz.saddr[3]);
  265.  
  266.  sprintf(d_addr,"%u.%u.%u.%u",
  267.                 infoz.daddr[0],
  268.                 infoz.daddr[1],
  269.                 infoz.daddr[2],
  270.                 infoz.daddr[3]);
  271.  
  272.  GREEN;     printf("%s\n",date);
  273.  BLUE;         printf("%s",s_addr);
  274.  GREEN;        printf(" -> ");
  275.  MAGENTA;    printf("%s",d_addr);
  276.  
  277.  if(infoz.protocol!=IPPROTO_ICMP) { 
  278.   if((proto=getprotobynumber(infoz.protocol))) { 
  279.    BLUE;       
  280.    if((service=getservbyport(infoz.source,proto->p_name)))
  281.           printf(" %s",service->s_name);
  282.    else     printf(" %d",infoz.source);
  283.    YELLOW;     printf(" / ");
  284.    MAGENTA;
  285.    if((service=getservbyport(infoz.dest,proto->p_name)))
  286.             printf("%s",service->s_name);
  287.    else     printf("%d",infoz.dest);
  288.   }
  289.  }
  290.  
  291.  
  292. #ifndef DONT_LOOKUP 
  293.  BLUE; 
  294.  
  295.  // limit shit for dns ... invoked with gethostbyaddr() 
  296.  // you can change it as you want ...
  297.   
  298.  switch(infoz.protocol) { 
  299.   case IPPROTO_TCP:
  300.                 if(infoz.flags==SYN)
  301.                   yes_lookup=1;
  302.              break;
  303.   case IPPROTO_UDP:
  304.              if(infoz.source != NAMESERVER_PORT &&
  305.                 infoz.dest   != NAMESERVER_PORT)
  306.               yes_lookup=1;
  307.              break;
  308.   case IPPROTO_ICMP:
  309.              yes_lookup=1;
  310.              break;
  311.  }          
  312.   
  313.  if(yes_lookup) {
  314.   RETURN;    
  315.   
  316.   inet_aton((char *)s_addr, &in_a);
  317.   if(!(hst_saddr=hostLookup(in_a)))
  318.           printf("none -> ");
  319.   else
  320.           printf("%s -> ",hst_saddr);
  321.  
  322.   MAGENTA;
  323.            
  324.   inet_aton((char *)d_addr, &in_a);
  325.   if(!(hst_daddr=hostLookup(in_a)))
  326.           printf("none");
  327.   else
  328.           printf("%s",hst_daddr);
  329.  }  
  330. #endif
  331.  MAGENTA; RETURN;
  332.  scan(infoz);    
  333. }
  334.  
  335.  
  336.  
  337. void scan(struct packet_info infoz)
  338. {
  339.  FILE *iff, *of;
  340.  char buf[512];
  341.  char o[400],tmp_port[10]; 
  342.  char *flags=NULL;
  343.  int HST_FOUND=0;
  344.  
  345.  if(!(iff=fopen(CONF,"r")))
  346.    return;
  347.  while(fgets(buf,512,iff)) {
  348.   if(buf[strlen(buf)-1]=='\n')
  349.    buf[strlen(buf)-1]=0;
  350.  
  351.   HST_FOUND = 0;
  352.   
  353.   if(infoz.version!=4) {
  354.    CLEAR;
  355.    printf("Sorry this is only a ipv4 version. write to: pigpen@s0ftpj.org\n");
  356.    printf("if you wanna ipv6 support\n");
  357.    exit(-1);
  358.   } 
  359.  
  360.   if(port && infoz.dest!=port && infoz.source!=port)
  361.    return;
  362.  
  363.   if(port && (infoz.protocol==IPPROTO_ICMP))
  364.    return;  
  365.  
  366.   if(hst_saddr) {
  367.    if(strstr(hst_saddr,buf)) 
  368.     HST_FOUND=1;
  369.   }
  370.             
  371.   if(hst_daddr) {    
  372.    if(strstr(hst_daddr,buf))
  373.    HST_FOUND=1;
  374.   }
  375.  
  376.     
  377.   if( strstr(s_addr, buf) || strstr(d_addr, buf) || HST_FOUND ) {    
  378. #ifdef SYSTEM_LOG
  379.    syslog(LOG_TYPE,"G0RK: My lord %s is here ... I log it", s_addr);
  380. #endif
  381.    if(buf[0]=='.' && buf[1]=='\0')
  382.     of=fopen(LOG_ALL,"a");
  383.    else
  384.     (buf[0]=='.') ? bcopy(buf+1,o,sizeof(o)) : bcopy(buf,o,sizeof(o));
  385.    if(port) { sprintf(tmp_port,":%d",port); strncat(o,tmp_port,sizeof(o)); }
  386.    of=fopen(o,"a+");
  387.    if(!of) {
  388.     CLEAR;
  389.     printf("Can't open %s file\n\n\n",buf);
  390.     return ;
  391.    }
  392.    fprintf(of,"<--->\n");
  393.    fprintf(of,"date: %s\n",date);
  394.    fprintf(of,"src:%s (%s)\ndst:%s (%s)\n",s_addr,hst_saddr,d_addr,hst_daddr);
  395.    if((infoz.protocol)!=IPPROTO_ICMP)
  396.     fprintf(of,"port: %d:%d\n",infoz.source,infoz.dest);
  397.     
  398.    switch(infoz.protocol) {
  399.     case IPPROTO_ICMP:
  400.      fprintf(of,"type: ");
  401.      switch((infoz.type)/256) {
  402.       case 0:
  403.        fprintf(of,"icmp echo reply\n");
  404.        break;
  405.       case 3:
  406.        fprintf(of,"icmp dest_unreach\n");
  407.        break;
  408.       case 4:
  409.        fprintf(of,"icmp source quench\n");
  410.        break;
  411.       case 5:
  412.        fprintf(of,"icmp redirect\n");
  413.        break;
  414.       case 8:
  415.        fprintf(of,"icmp echo\n");
  416.        break;
  417.       case 11:
  418.        fprintf(of,"icmp time exceeded\n");
  419.        break;
  420.       case 12:
  421.        fprintf(of,"icmp parameter problem\n");
  422.        break;
  423.       case 13:
  424.        fprintf(of,"icmp timestamp\n");
  425.        break;
  426.       case 14:
  427.        fprintf(of,"icmp timestamp reply\n");
  428.        break;
  429.       case 15:
  430.        fprintf(of,"icmp information\n");
  431.        break;
  432.       case 16:
  433.        fprintf(of,"icmp information reply\n");
  434.        break;
  435.       case 17:
  436.        fprintf(of,"icmp address mask\n");
  437.        break;
  438.       case 18:
  439.        fprintf(of,"icmp address mask reply\n");
  440.        break;
  441.       default:
  442.        fprintf(of,"icmp type %i\n", infoz.type);
  443.        break;
  444.      }
  445.      break;  
  446.     
  447.     case IPPROTO_TCP:
  448.      fprintf(of,"seq #: %u\n",(unsigned int) infoz.seq);
  449.      fprintf(of,"ack #: %u\n",(unsigned int) infoz.ack_seq);
  450.      fprintf(of,"ttl: %i\n",infoz.ttl);
  451.      fprintf(of,"win: %i\n",infoz.window);
  452.      
  453.      switch (infoz.flags) {
  454.       case URG:
  455.        flags="-----U";
  456.        break;
  457.       case ACK_PSH:
  458.        flags="---PA-";
  459.        break;
  460.       case SYN_ACK:
  461.        flags="-S--A-";
  462.        break;
  463.       case FIN_ACK:
  464.        flags="F---A-";
  465.        break;
  466.       case ACK:
  467.        flags="----A-";
  468.        break;
  469.       case PSH:
  470.        flags="---P--";
  471.        break;
  472.       case RST:
  473.        flags="--R---";
  474.        break;
  475.       case SYN:
  476.        flags="-S----";
  477.        break;
  478.       case FIN:
  479.        flags="F-----";
  480.        break;
  481.      }
  482.      fprintf(of,"flags %s\n",flags);    
  483.      break;
  484.     case IPPROTO_UDP:
  485.      if(infoz.dest==SYSLOG_PORT && port!=SYSLOG_PORT)
  486.       fprintf(of,"SYSLOG DATA: %s\n",infoz.dataload); 
  487.      break;
  488.    }    
  489.     
  490.    buf[strlen(buf)+1]=0;
  491.    buf[strlen(buf)]='\n';
  492.    fclose(of);
  493.   }
  494.  }
  495.  fclose(iff);
  496. }
  497.  
  498. void pcap_device_on(void)
  499. {
  500.  char            errbuf[400];
  501.  int datalink;
  502.  
  503.  if (!deviceglobal || !strcmp(deviceglobal, "default")) {
  504.   deviceglobal=pcap_lookupdev(errbuf);
  505.   printf("Device ->");
  506.   GREEN;
  507.   printf(" %s.\n\n", deviceglobal);
  508.  }
  509.  
  510.  if (!deviceglobal) {
  511.   printf("Error getting device - %s\n", errbuf);
  512.   exit(1);
  513.  }
  514.  
  515.  pcap_global_descriptor =
  516.   pcap_open_live(deviceglobal, 68, PROMISC, 1000, errbuf);
  517.  
  518.  if (!pcap_global_descriptor) {
  519.   printf("error opening pcap: %s\n", errbuf);
  520.   exit(1);
  521.  }
  522.  
  523.  datalink = pcap_datalink(pcap_global_descriptor);
  524.  
  525.  switch (datalink) {
  526.   case DLT_EN10MB:
  527.                   offset = 14;
  528.                   break;
  529.   case DLT_NULL:
  530.   case DLT_PPP:    
  531.                   offset = 4;
  532.                   break;
  533.   case DLT_SLIP:
  534.                   offset = 16;
  535.                   break;
  536.   case DLT_RAW:
  537.           offset = 0;
  538.           break;
  539.   case DLT_SLIP_BSDOS:
  540.   case DLT_PPP_BSDOS:
  541.           offset = 24;
  542.           break;
  543.   default:
  544.                   printf("unknown datalink type (%d)", datalink);
  545.                   exit(-1); 
  546.  }
  547. }
  548.  
  549.  
  550. void sniff_pk(struct packet_info *infoz)
  551. {
  552.  struct ip          *IP;
  553.  struct TCPhdr      *TCP;
  554.  struct udphdr      *UDP;
  555.  struct icmp        *ICMP;
  556.  struct pcap_pkthdr lpcap_hdr;
  557.  char     *sniff_buff;
  558.  
  559.  bzero(s_addr, sizeof(s_addr));
  560.  bzero(d_addr, sizeof(d_addr));
  561.  
  562.  if((sniff_buff=(char *) pcap_next(pcap_global_descriptor, &lpcap_hdr))){
  563.   (char *) sniff_buff+=offset;
  564.   IP = (struct ip *) sniff_buff;
  565.   infoz->ttl = IP->ip_ttl;
  566.   infoz->protocol = (char)IP->ip_p;
  567.   infoz->version  = (char)IP->ip_v;
  568.   infoz->saddr = (unsigned char *)&(IP->ip_src.s_addr);
  569.   infoz->daddr = (unsigned char *)&(IP->ip_dst.s_addr);
  570.  
  571.   switch (infoz->protocol) {
  572.    case IPPROTO_TCP:
  573.     TCP = (struct TCPhdr *)(sniff_buff+sizeof(*IP));
  574.     infoz->seq     = ntohl(TCP->seq);
  575.     infoz->ack_seq = ntohl(TCP->ack_seq);
  576.     infoz->source  = ntohs(TCP->source);
  577.     infoz->dest    = ntohs(TCP->dest);
  578.     infoz->window  = ntohs(TCP->window);
  579.     infoz->flags   = ntohs(TCP->offset_flag)&
  580.      (URG|ACK|PSH|FIN|RST|SYN);
  581.     memcpy(infoz->dataload,
  582.            sniff_buff + sizeof(struct ip) + sizeof(struct TCPhdr),
  583.            ntohs(IP->ip_len)-sizeof(struct ip)-sizeof(struct TCPhdr));
  584.     break;
  585.    case IPPROTO_UDP:
  586.     UDP = (struct udphdr *)(sniff_buff+sizeof(*IP));
  587.     infoz->source = ntohs(UDP->uh_sport);
  588.     infoz->dest   = ntohs(UDP->uh_dport);
  589.     memcpy(infoz->dataload,
  590.            sniff_buff + sizeof(struct ip) + sizeof(struct udphdr),
  591.            ntohs(IP->ip_len)-sizeof(struct ip)-sizeof(struct udphdr));
  592.     break;
  593.    case IPPROTO_ICMP:
  594.     ICMP = (struct icmp *)(sniff_buff+sizeof(*IP));
  595.     infoz->type = ntohs(ICMP->icmp_type);
  596.     infoz->id   = ntohs(ICMP->icmp_seq);
  597.     break;
  598.   }
  599.  }
  600. }
  601.  
  602.  
  603. void ethclose()
  604. {
  605.  if(pcap_global_descriptor) pcap_close(pcap_global_descriptor);
  606.  MAGENTA;
  607.  printf("I will getby ... I will survive...\n");
  608.  WHITE;
  609.  exit(0);
  610. }
  611.  
  612.  
  613. void dump_tcp(struct packet_info info, int data)
  614. {
  615.  char *flags=NULL;
  616.  
  617.  print_addr(info);
  618.  MAGENTA;
  619.  printf("TCP ");
  620.  BLUE;
  621.  printf("%u:", (unsigned int) info.seq);
  622.  MAGENTA;
  623.  printf("%u", (unsigned int) info.ack_seq);
  624.  MAGENTA; printf("\tTTL: ");
  625.  BLUE;    printf("%i ", info.ttl);
  626.  MAGENTA; printf("\tWin: ");
  627.  BLUE;    printf("%i", info.window);    
  628.  
  629.  switch (info.flags) {
  630.   case URG:
  631.    flags="-----\033[1;32mU\033[1;34m";
  632.    break;
  633.   case ACK_PSH:
  634.    flags="---\033[1;32mPA\033[1;34m-";
  635.    break;
  636.   case SYN_ACK:
  637.    flags="-\033[1;32mS\033[0;34m--\033[1;32mA\033[1;34m-";
  638.    break;
  639.   case FIN_ACK:
  640.    flags="\033[1;32mF\033[1;34m---\033[1;32mA\033[1;34m-";
  641.    break;
  642.   case ACK:
  643.    flags="----\033[1;32mA\033[1;34m-";
  644.    break;
  645.   case PSH:
  646.    flags="---\033[1;32mP\033[1;34m--";
  647.    break;
  648.   case RST:
  649.    flags="--\033[1;32mR\033[1;34m---";
  650.    break;
  651.   case SYN:
  652.    flags="-\033[1;32mS\033[1;34m----";
  653.    break;
  654.   case FIN:
  655.    flags="\033[1;32mF\033[1;34m-----";
  656.    break;
  657.  }
  658.  
  659.  MAGENTA;  printf("     FLAGS: ");
  660.  BLUE; printf("%s\n",flags);
  661.  if(data && (info.flags==PSH || info.flags==ACK_PSH)) {
  662.    BLUE;   printf("-> ");
  663.    GREEN;  printf("%s\n",info.dataload);
  664.  }
  665.  LINE;
  666. }
  667.  
  668.  
  669.  
  670. void dump_udp(struct packet_info info)
  671. {
  672.  FILE *fp_sl;
  673.  struct DNSpkt *dns_pkt;
  674.  
  675.  print_addr(info);
  676.  printf("UDP ");
  677.  
  678.  if(info.dest==SYSLOG_PORT && port!=SYSLOG_PORT) {
  679.   GREEN; 
  680.   printf("%s", info.dataload);
  681.   if(syslog_string && info.dataload) {
  682.    if(strstr(info.dataload,syslog_string)) {
  683.     RED;
  684.     printf("\tMSG LOGGED -> %s\n",LOG_SL);
  685.     fp_sl=fopen(LOG_SL,"a");
  686.     now=time(NULL);
  687.     strftime(date,60,"%H:%M:%S %a %h %d", localtime(&now));
  688.     fprintf(fp_sl,"\n%s --- str -> %s\n",date,syslog_string); 
  689.     fprintf(fp_sl,"%s -> %s == %s\n", 
  690.                    s_addr,
  691.                    d_addr,
  692.                    info.dataload);
  693.     fclose(fp_sl);
  694.    }
  695.   }
  696.  } 
  697.  
  698.  if(info.source==NAMESERVER_PORT || info.dest==NAMESERVER_PORT) { 
  699.   dns_pkt=(struct DNSpkt *) info.dataload;
  700.  
  701.   BLUE;        printf("\tRD: ");
  702.   MAGENTA;    printf("%d ",dns_pkt->head.rd);
  703.   BLUE;        printf("AA: ");
  704.   MAGENTA;    printf("%d ",dns_pkt->head.aa);
  705.   BLUE;        printf("OPCODE: ");
  706.   MAGENTA;
  707.   
  708.   switch(dns_pkt->head.opcode) {
  709.    case  QUERY: printf("QUERY ");  break;
  710.    case IQUERY: printf("IQUERY "); break;
  711.    case STATUS: printf("STATUS "); break;
  712.        default: printf("%d ",dns_pkt->head.opcode);  
  713.   }
  714.   
  715.   BLUE;        printf("QR: ");
  716.   MAGENTA;    printf("%d ",dns_pkt->head.qr);
  717.  
  718.   BLUE;        printf("RA: ");
  719.   MAGENTA;    printf("%d ",dns_pkt->head.ra);
  720.   BLUE;        printf("AD: ");
  721.   MAGENTA;    printf("%d ",dns_pkt->head.ad);
  722.   BLUE;        printf("CD: ");
  723.   MAGENTA;    printf("%d",dns_pkt->head.cd);
  724.   BLUE;        printf("\tDNSPKT ID: ");
  725.   RED;        printf("%d",dns_pkt->head.id);
  726.  }
  727.  RETURN; BLUE; LINE;
  728. }
  729.  
  730.  
  731.  
  732. void dump_icmp(struct packet_info info)
  733. {
  734.  print_addr(info);
  735.  
  736.  MAGENTA; printf("ICMP ");
  737.  BLUE;      printf("TYPE: ");
  738.  RED;
  739.  
  740.  switch((info.type/256)) {
  741.   case 0:
  742.          printf("echo reply\t");
  743.          break;
  744.   case 3:
  745.          printf("dest_unreach\t");
  746.          break;
  747.   case 4:
  748.          printf("source quench\t");
  749.          break;
  750.   case 5:
  751.          printf("redirect\t");
  752.          break;
  753.   case 8:
  754.          printf("echo\t");
  755.          break;
  756.   case 11:
  757.          printf("time exceeded\t");
  758.          break;
  759.   case 12:
  760.          printf("parameter problem\t");
  761.          break;
  762.   case 13:
  763.          printf("timestamp\t");
  764.          break;
  765.   case 14:
  766.          printf("timestamp reply\t");
  767.          break;
  768.   case 15:
  769.          printf("information\t");
  770.          break;
  771.   case 16:
  772.          printf("information reply\t");
  773.          break;
  774.   case 17:
  775.          printf("address mask\t");
  776.          break;
  777.   case 18:
  778.          printf("address mask reply\t");
  779.          break;
  780.   default:
  781.          printf("%i\t", info.type);
  782.          break;
  783.  }
  784.  
  785.  BLUE;
  786.  printf("(ttl:%i id:%i)\n", info.ttl, (info.id/256));
  787.  LINE;
  788. }
  789.  
  790.  
  791. int main(int argc, char **argv)
  792. {
  793.  int snoop = 0, opt;
  794.  struct packet_info pk_info;
  795.  
  796.  signal(SIGINT,  ethclose);
  797.  signal(SIGTERM, ethclose);
  798.  signal(SIGKILL, ethclose);
  799.  signal(SIGQUIT, ethclose);
  800.  
  801.  fuckin_about_all_day(); 
  802.  
  803.  while ((opt = getopt(argc, (char **) argv, "vhp:l:i:")) != EOF) {
  804.   switch(opt) {
  805.    case 'v':
  806.             snoop=1;
  807.             break;
  808.    case 'h':
  809.             usage(argv[0]);
  810.         exit(1);
  811.    case 'p':
  812.             if(syslog_string) {
  813.          printf("Can't execute gork with -l & -p\n");
  814.          WHITE;
  815.          exit(1);
  816.         }
  817.         port=atoi(optarg);
  818.         break;
  819.    case 'l':
  820.             if(port) {
  821.          printf("Can't execute gork with -p & -l\n");
  822.          WHITE;
  823.          exit(1);
  824.         } 
  825.             syslog_string=optarg;
  826.         break;    
  827.    case 'i':
  828.             deviceglobal=optarg;
  829.             break;
  830.     default:
  831.             exit(1);
  832.   }
  833.  }
  834.  
  835.  pcap_device_on();
  836.  
  837.  while(1) {
  838.   bzero(&pk_info,sizeof(pk_info));
  839.   sniff_pk(&pk_info);
  840.  
  841.   // add here other protocol implementations with their functions 
  842.   
  843.   switch(pk_info.protocol) {
  844.    case IPPROTO_TCP:
  845.                      dump_tcp(pk_info, snoop);
  846.                      break;
  847.    case IPPROTO_UDP:
  848.                      dump_udp(pk_info);
  849.                      break;
  850.    case IPPROTO_ICMP:
  851.                      dump_icmp(pk_info);
  852.                      break;
  853.   }
  854.  }
  855. }
  856.